// hyb_yl/history_list/history_list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    a: false,
    edit: false,
    editCon: "编辑",
    none: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
  },
  checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var that = this
    if (e.detail.value.length == 0) {
      var a = false
    } else {
      var a = true
    }
    that.setData({
      a: a
    })
  },
  del: function(e) {
    // console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index
    var historyArr = that.data.historyArr
    if (historyArr[index].checked == 0) {
      historyArr[index].checked = 1
    } else {
      historyArr[index].checked = 0
    }
    console.log(historyArr)
    that.setData({
      historyArr: historyArr
    })
  },
  subClick: function(e) {
    // console.log(e)
    var that = this
    var historyArr = that.data.historyArr
    console.log(historyArr)

    var gid = e.detail.value.checkBox
    app.util.request({
      'url': 'entry/wxapp/Deletinfo',
      data: {
        gid: gid
      },
      success: function(res) {
        var arrs = res.data.data;

        console.log(res);

      },
      fail: function(err) {
        console.log(err)
      },

    })
    for (var i = 0, len = historyArr.length; i < len; i++) {
      console.log(historyArr[i])
      if (historyArr[i].checked == 1) {
        historyArr.splice(i, 1)
        i--
        len--
      }
    }
    that.setData({
      historyArr: historyArr
    })
  },
  //编辑 取消
  editClick: function() {
    var that = this
    if (that.data.edit) {
      var editCon = "编辑"
    } else {
      var editCon = "取消"
      var a = false
    }
    that.setData({
      edit: !that.data.edit,
      a: a
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getHistoryArr()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getHistoryArr: function() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/HistoryArr',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        console.log(res)
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].checked = 0
        }
        that.setData({
          historyArr: res.data.data
        })
      }
    })
  },
  pay: function(e) {
    var that = this
    var money = e.currentTarget.dataset.money
    var openid = wx.getStorageSync('openid')
    var types = e.currentTarget.dataset.type
    var qid = e.currentTarget.dataset.qid
    var q_category = e.currentTarget.dataset.data
    //查询问题详情
    app.util.request({
      'url': 'entry/wxapp/Inforquestion',
      data: {
        qid: qid
      },
      success: function(res) {
        console.log(res)
        var zid = res.data.data.p_id
        var user_openid = res.data.data.user_openid
        var fromuser = res.data.data.fromuser
        var qid = res.data.data.qid
        if (types == 1) {
          wx.navigateTo({
            url: '/hyb_yl/userLife/pages/zhuan_liao/zhuan_liao?zid=' + zid + '&user_openid=' + user_openid + '&q_category=' + q_category + '&qid=' + qid + '&fromuser=' + fromuser,
          })
        } else {

          app.util.request({
            'url': 'entry/wxapp/Pay',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: openid,
              z_tw_money: money
            },
            success: function(res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function(res) {
                  app.util.request({
                    'url': "entry/wxapp/Goodsinfo",
                    data: {
                      money: money,
                      qid: qid,
                      openid: openid,
                      type1: 1
                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function(res) {
                      console.log(res)
                      wx.navigateTo({
                        url: '/hyb_yl/userLife/pages/zhuan_liao/zhuan_liao?zid=' + zid + '&user_openid=' + user_openid + '&q_category=' + q_category + '&qid=' + qid + '&fromuser=' + fromuser,
                      })
                    },
                    fail: function(err) {
                      console.log(err)
                    },
                  })
                }
              })
            },
            fail: function(err) {
              console.log(err);
            }
          })
        }
      }
    })
  }
})